package edu.nctu.csie.jichang.dp.behavioral;

import java.util.Hashtable;

public class Mediator {
	public static void main(String[] args) {
		ConcreteMediator tMediator = new ConcreteMediator();
		ColleagueA tColleagueA = new ColleagueA(tMediator,"A");
		ColleagueB tColleagueB = new ColleagueB(tMediator,"B");
		tColleagueA.call("B");
		tColleagueB.call("A");
	}
}
interface IMediator {
	public void register(String name, AbstractColleague c);
	public void call(String name, AbstractColleague from);
} 
class ConcreteMediator implements IMediator{
	Hashtable<String, AbstractColleague> colleagues = new Hashtable<String, AbstractColleague>();
	public void register(String name, AbstractColleague c) {colleagues.put(name, c);}
	public void call(String name, AbstractColleague from) {
		AbstractColleague c = colleagues.get(name);
		if(c!=null){
			System.out.println(from.name + " call " + c.name);
			c.doExecute();
		}
	}
}
abstract class AbstractColleague{
	String name;    
	IMediator med;
	AbstractColleague(IMediator med,String name){
		this.name = name;
		this.med  = med;
		this.med.register(this.name, this);
	}
	public void call(String name) {this.med.call(name, this);}
	abstract void doExecute();
}
class ColleagueA extends AbstractColleague {
	ColleagueA(IMediator med, String name) {super(med, name);}
	public void doExecute() {System.out.println("Colleague A do execute!");}
}
class ColleagueB extends AbstractColleague {
	ColleagueB(IMediator med, String name) {super(med, name);}
	public void doExecute() {System.out.println("Colleague B do execute!");}
}